package cn.tyf.service.imp;

import cn.tyf.dao.RecordDao;
import cn.tyf.domain.PageBean;
import cn.tyf.domain.Record;
import cn.tyf.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecordServiceImp implements RecordService {
    @Autowired
    @Qualifier("recordDao")
    private RecordDao recordDao;

    @Autowired
    @Qualifier("pageBean")
    private PageBean pageBean;
    @Override
    public PageBean findByPage(String name,Integer currentPage,Integer pageSize) {
        //1总记录数
        Integer count = 0;
        if (!name.equals("null") &&  name != null && !name.isEmpty()){
            count = recordDao.findByNameCount(name);
        }else {
            count = recordDao.findByCount();
        }
        pageBean.setTotalCount(count);
        //总页数
        Integer totalPage = (count % pageSize==0) ? count / pageSize : count / pageSize +1;
        pageBean.setTotalPage(totalPage);
        //当前页码
        pageBean.setCurrentPage(currentPage);
        //每页记录数
        pageBean.setPageSize(pageSize);
        //数据
        int start = (currentPage - 1) * pageSize;

        List<Record> byPage = null;

        if(!name.equals("null") && name != null && !name.isEmpty()){
            byPage = recordDao.findByNamePage(name,start, pageSize);
        }else {
            byPage = recordDao.findByPage(start, pageSize);
        }

        pageBean.setList(byPage);
        return pageBean;
    }


    @Override
    public PageBean<Record> findPageByUser(String rid,String time, Integer currentPage, Integer pageSize) {
        //1总记录数
        Integer count = recordDao.findByUserCount(rid, time);
        pageBean.setTotalCount(count);
        //总页数
        Integer totalPage = (count % pageSize==0) ? count / pageSize : count / pageSize +1;
        pageBean.setTotalPage(totalPage);
        //当前页码
        pageBean.setCurrentPage(currentPage);
        //每页记录数
        pageBean.setPageSize(pageSize);
        //数据
        int start = (currentPage - 1) * pageSize;
        List<Record> byPage = recordDao.findPageByUser(rid,time,start,pageSize);
        pageBean.setList(byPage);
        return pageBean;
    }

    @Override
    public void addAnnal(String card, String time, Integer door) {
        recordDao.addAnnal(card,time,door);
    }

    @Override
    public List<Record> findAll() {
        return recordDao.findAll();
    }
}
